package com.woniu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.entity.*;
import com.woniu.pojo.*;
import com.woniu.pojo.Class;
import com.woniu.service.*;
import com.woniu.utils.TdExcelUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller

@RequestMapping("attendence")
public class AttendenceController {
    @Autowired
    AttendenceService attendenceService;
    //学生类
    @Autowired
    StudentService studentService;
    //科目
    @Autowired
    CourseService courseService;
    //老师
    @Autowired
    EmpService empService;
    //班级
    @Autowired
    ClassService classService;
    //课程表
    @Autowired
    CoursetableService coursetableService;
    //员工角色表
    @Autowired
    EmpRoleService empRoleService;
    @Autowired
    TeacherCourseService teacherCourseService;
    /**
     *   展示跳转,根据角色不同，跳转不同界面
     */
    @RequestMapping("listJump")
    public String listJump(Model model, HttpSession session){
        User1 user=(User1)session.getAttribute("user");
        //员工跳转页面
        if(user.getRoleStatus().equals("1")){
            //根据员工id-->在员工角色表emprole找到emppro对象-->找到roleId——>在角色表找到角色名字-->判断
            //根据员工id-->在员工角色表emprole1找到emppro1对象-->找到角色名字判断
            List<Emprole1> isEmpRole = empRoleService.findIsEmpRole(user.getUid());
            String empRole=null;
            for (Emprole1 e : isEmpRole) {
                if (e.getRoleId().getRoleName().equals("讲师")) {
                    empRole="讲师";
                }else if (e.getRoleId().getRoleName().equals("教学主管")) {
                    empRole="教学主管";
                    break;
                }else if (e.getRoleId().getRoleName().equals("班主任")) {
                    empRole="班主任";
                }else if (e.getRoleId().getRoleName().equals("就业专员")) {
                    empRole="就业专员";
                    break;
                }
            }
            model.addAttribute("empRole", empRole);
        }
        return "WEB-INF/attendence/empFindAttendence11";
    }
    /**
     * 学生考勤跳转页面
     */
    @RequestMapping("studentAttendencelistJump")
    public String studentAttendencelistJump(Model model, HttpSession session){
        User1 user=(User1)session.getAttribute("user");
        if (user.getRoleStatus().equals("2")) {
            //是学生跳转学生页面
            //根据学生id-->找到学生对象-->找到班级id-->找到课程表对象集合-->找到集合中课程id封装成集合-->在课程表找到对应的课程对象集合，放到Model
            System.out.println(user.getUid());
            Student student = studentService.selectOne(user.getUid());
            List<Coursetable> coursetableList = coursetableService.findByClassId(student.getClassId());
            List<Integer> courseIdList = new ArrayList<>();
            for (Coursetable c : coursetableList) {
                //遍历集合把id放到集合中
                courseIdList.add(c.getCourseId());
            }
            List<Course> courseList2 = courseService.selectByCourseIdList(courseIdList);
            model.addAttribute("courseList2", courseList2);
        }
        return "WEB-INF/attendence/studentFindAttendence";
    }
    /**
     * 班主任考勤跳转页面
     */
    @RequestMapping("BzrAttendencelistJump")
    public String BzrAttendencelistJump(Model model, HttpSession session) {
        User1 user = (User1) session.getAttribute("user");
        //员工跳转页面
        if (user.getRoleStatus().equals("1")) {
            //根据员工id-->在员工角色表emprole找到emppro对象-->找到roleId——>在角色表找到角色名字-->判断
            //根据员工id-->在员工角色表emprole1找到emppro1对象-->找到角色名字判断
            List<Emprole1> isEmpRole = empRoleService.findIsEmpRole(user.getUid());
            String empRole = null;
            for (Emprole1 e : isEmpRole) {
                if (e.getRoleId().getRoleName().equals("班主任")) {
                    empRole = "班主任";
                    break;
                }
            }
            model.addAttribute("empRole", empRole);
        }
        return "WEB-INF/attendence/empFindAttendence12";
    }
    /**
     * 讲师考勤跳转页面
     */
    @RequestMapping("teacherAttendencelistJump")
    public String teacherAttendencelistJump(Model model, HttpSession session) {
        User1 user = (User1) session.getAttribute("user");
        //员工跳转页面
        if (user.getRoleStatus().equals("1")) {
            //根据员工id-->在员工角色表emprole找到emppro对象-->找到roleId——>在角色表找到角色名字-->判断
            //根据员工id-->在员工角色表emprole1找到emppro1对象-->找到角色名字判断
            List<Emprole1> isEmpRole = empRoleService.findIsEmpRole(user.getUid());
            String empRole = null;
            for (Emprole1 e : isEmpRole) {
                if (e.getRoleId().getRoleName().equals("讲师")) {
                    empRole = "讲师";
                    break;
                }
            }
            model.addAttribute("empRole", empRole);
        }
        return "WEB-INF/attendence/empFindAttendence11";
    }

    /**
     * 找到班级id-->找到课程表对象集合-->找到集合中课程id封装成集合-->在课程表找到对应的课程对象集合，放到Model
     * 根据讲师id-->在讲师课程表里找到,所交的班级和对应的课程
     */
    @RequestMapping("selectCCByTeacherId")
    @ResponseBody
    public Map<String,Object> selectCCByTeacherId(HttpSession session,Teachercourse teachercourse){
        Map<String, Object> map=new HashMap<String,Object>();
        System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
        System.out.println(teachercourse);
        try {
            List<Teachercourse1> teachercourseList = teacherCourseService.findByExample(teachercourse);
            map.put("teachercourseList",teachercourseList);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return  map;
    }
    /**
     * 根据班主任id-->在班级表里找到班级对象-->，招到课程-->放入Model
     */
    @RequestMapping("selectCourseByEmpId")
    @ResponseBody
    public Map<String,Object> selectCourseByEmpId(HttpSession session){
        User1 user=(User1)session.getAttribute("user");
        Map<String, Object> map=new HashMap<String,Object>();
        try {
            List<Class> classList= classService.selectEmpByTeacherId(user.getUid());
            Class aClass = classList.get(0);
            //根据班级id，在讲师课程表，找到讲师,找到课程
            Teachercourse teachercourse=new Teachercourse();
            teachercourse.setClassId(aClass.getClassId());
            List<Teachercourse1> teachercourseList = teacherCourseService.findByExample(teachercourse);
            map.put("teachercourseList",teachercourseList);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return  map;
    }
    /**
     * 增加页面跳转
     */
    @RequestMapping("addJump")
    public String addJump(Model model,HttpSession session){
        User1 user=(User1)session.getAttribute("user");
        //根据班主任id，在班级表找到班级对象，
        List<Class> classList = classService.selectEmpByTeacherId(user.getUid());
        //班主任1对1,班级对象
        Class aClass = classList.get(0);
        //根据班级id，找到所有学生
        Student student=new Student();
        student.setClassId(aClass.getClassId());
        List<Student> studentList = studentService.selectExample(student);
        //根据班级id，在讲师课程表，找到讲师,找到课程
        Teachercourse teachercourse=new Teachercourse();
        teachercourse.setClassId(aClass.getClassId());
        List<Teachercourse1> teachercourseList = teacherCourseService.findByExample(teachercourse);
        model.addAttribute("teachercourseList",teachercourseList);
        model.addAttribute("aClass",aClass);
        model.addAttribute("studentList",studentList);
        return "WEB-INF/attendence/add";
    }

    /**
     * 改考勤结果修
     * @return
     */
    @RequestMapping("updateResultById")
    @ResponseBody
    public Map<String, Object> updateResultById(Integer attendenceId){
        Map<String, Object> map=new HashMap<String,Object>();
        try {
            String str= attendenceService.updateResultById(attendenceId);
            map.put("status",str);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return  map;
    }

    //增加考勤
    @RequestMapping("add")
    @ResponseBody
    public Map<String,Object> add(Attendence attendence){
        Map<String, Object> map=new HashMap<String,Object>();
        try {
           attendenceService.add(attendence);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return  map;
    }
    //按住键删除考勤
    @RequestMapping("deleteById")
    @ResponseBody
    public Map<String,Object> deleteById(Integer attendenceId){
        Map<String, Object> map=new HashMap<String,Object>();
        try {
            attendenceService.deleteById(attendenceId);

            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return  map;
    }
    //查到一个考勤
    @RequestMapping("findOne")
    @ResponseBody
    public Map<String, Object> findOne(Integer attendenceId){
        Map<String, Object> map=new HashMap<String,Object>();
        try {
           Attendence1 attendence=attendenceService.findOne(attendenceId);

            System.out.println(attendence);
            map.put("attendence",attendence);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return  map;
    }
    //跳转到修改
    @RequestMapping("updateJump")
    public String updateJump(Model model, Integer attendenceId,HttpSession session){
        User1 user=(User1)session.getAttribute("user");
        //根据老师id，在班级表找到班级对象，
        List<Class> classList = classService.selectEmpByTeacherId(user.getUid());
        //班主任1对1,班级对象
        Class aClass = classList.get(0);
        //根据班级id，找到所有学生
        Student student=new Student();
        student.setClassId(aClass.getClassId());
        List<Student> studentList = studentService.selectExample(student);
        //根据班级id，在讲师课程表，找到讲师,找到课程
        Teachercourse teachercourse=new Teachercourse();
        teachercourse.setClassId(aClass.getClassId());
        List<Teachercourse1> teachercourseList = teacherCourseService.findByExample(teachercourse);
        model.addAttribute("teachercourseList",teachercourseList);
        model.addAttribute("aClass",aClass);
        model.addAttribute("studentList",studentList);

        Attendence attendence=attendenceService.selectOne(attendenceId);

        model.addAttribute("attendence",attendence);

        model.addAttribute("attendenceId",attendenceId);

        return "WEB-INF/attendence/update";
    }
    //修改考勤
    @RequestMapping("updateById")
    @ResponseBody
    public Map<String, Object> updateById(Attendence attendence){
        Map<String, Object> map=new HashMap<String,Object>();
        try {
           attendenceService.updateById(attendence);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return  map;
    }
    //考勤查询分页
    @RequestMapping("findAll")
    @ResponseBody
    public Map<String, Object> findAll( @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",defaultValue ="6") Integer pageSize){
        Map<String, Object> map=new HashMap<String,Object>();
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<Attendence1> attendenceList=attendenceService.findAll();
            PageInfo<Attendence1> pageInfo=new PageInfo<Attendence1>(attendenceList);
            map.put("pageInfo",pageInfo);
            map.put("attendenceList",attendenceList);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }

        return  map;
    }
    //考勤批量删除
    @RequestMapping("deleteByExample")
    @ResponseBody
    public Map<String,Object> deleteByExample(Integer[] attendenceIds){
        System.out.println(attendenceIds[0]);
        Map<String, Object> map=new HashMap<String,Object>();
        try {
            attendenceService.deleteByExample(Arrays.asList(attendenceIds));

            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return  map;
    }
    /*1.下拉菜单式，学生查询自己的考勤，传入学生id，可以按照课程id,日期id,
    2.老师查询，传入empId ,选择班级，传入班级Id，查询
    3.
     */
    @RequestMapping("findDate")
    @ResponseBody
    public Map<String, Object> findDate(Date start, Date end,Emp emp,Student student, Attendence attendence, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",defaultValue ="6") Integer pageSize){
        Map<String, Object> map=new HashMap<String,Object>();
        Map<String, Object> mapCondition=new HashMap<String,Object>();
        mapCondition.put("start",start);
        mapCondition.put("end",end);
        mapCondition.put("attendence",attendence);
        //按学生名字模糊查询,将学生Id放入集合
        List<Integer> studentIdList=new ArrayList<>();
        List<Student> studentList = studentService.selectExample(student);
        for (Student s:studentList) {
            studentIdList.add(s.getStudentId());
        }
        //按照老师名字查询，将老师id放入集合
        List<Integer> empIdList=new ArrayList<>();
        List<Emp> empList = empService.selectExample(emp);
        for (Emp e:empList) {
            empIdList.add(e.getEmpId());
        }
        mapCondition.put("studentIdList",studentIdList);
        mapCondition.put("empIdList",empIdList);
        System.out.println("---------------------------------------------");
        System.out.println(attendence);
        System.out.println("---------------------------------------------");
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<Attendence1> attendenceList=attendenceService.findDate(mapCondition);
            for (Attendence1 a: attendenceList  ) {
                System.out.println(a);
            }
            PageInfo<Attendence1> pageInfo=new PageInfo<Attendence1>(attendenceList);
            map.put("pageInfo",pageInfo);
            map.put("attendenceList",attendenceList);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return  map;
    }
    /**
     * 1.学生登录后，查看自己的考勤，且不能修改，删除自己的考勤
     */
    @RequestMapping("findAttendence")
    @ResponseBody
    public Map<String, Object> findAttendence(Date start, Date end, Attendence attendence,HttpSession session, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",defaultValue ="6") Integer pageSize){
        Map<String, Object> map=new HashMap<String,Object>();
        Map<String, Object> mapCondition=new HashMap<String,Object>();
        User1 user=(User1)session.getAttribute("user");
        //将登陆账户的Id,传入查询条件封装到attendence
        attendence.setStudentId(user.getUid());
        mapCondition.put("start",start);
        mapCondition.put("end",end);
        mapCondition.put("attendence",attendence);
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<Attendence1> attendenceList=attendenceService.findDate(mapCondition);
            PageInfo<Attendence1> pageInfo=new PageInfo<Attendence1>(attendenceList);
            map.put("pageInfo",pageInfo);
            map.put("attendenceList",attendenceList);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return  map;
    }
    /**
     ** 2.老师登陆后，传入老师id，老师
     */
    @RequestMapping("teacherFindAttendence")
    @ResponseBody
    public Map<String, Object> teacherFindAttendence(Date start, Date end, Attendence attendence,HttpSession session, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",defaultValue ="6") Integer pageSize){
        Map<String, Object> map=new HashMap<String,Object>();
        Map<String, Object> mapCondition=new HashMap<String,Object>();
        User1 user=(User1)session.getAttribute("user");
        attendence.setEmpId(user.getUid());
        mapCondition.put("start",start);
        mapCondition.put("end",end);
        mapCondition.put("attendence",attendence);

        try {
            PageHelper.startPage(pageNum,pageSize);
            List<Attendence1> attendenceList=attendenceService.findDate(mapCondition);
            PageInfo<Attendence1> pageInfo=new PageInfo<Attendence1>(attendenceList);
            map.put("pageInfo",pageInfo);
            map.put("attendenceList",attendenceList);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return  map;
    }
    /**
     ** 2.班主任登陆后，传入老师id，老师
     */
    @RequestMapping("BzrFindAttendence")
    @ResponseBody
    public Map<String, Object> BzrFindAttendence(Date start, Date end, Attendence attendence,HttpSession session, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",defaultValue ="6") Integer pageSize){
        Map<String, Object> map=new HashMap<String,Object>();
        Map<String, Object> mapCondition=new HashMap<String,Object>();
        User1 user=(User1)session.getAttribute("user");
        List<Class> classList= classService.selectEmpByTeacherId(user.getUid());
        Class aClass = classList.get(0);
        attendence.setClassId(aClass.getClassId());
        Teachercourse teachercourse=new Teachercourse();
        teachercourse.setEmpId(user.getUid());
        List<Teachercourse1> teachercourseList = teacherCourseService.findByExample(teachercourse);
        List<Integer> classIdList=new ArrayList<>();
        for (Teachercourse1 t:teachercourseList) {
            classIdList.add(t.getClassId().getClassId());
        }
//        mapCondition.put("classIdList",classIdList);

        mapCondition.put("start",start);
        mapCondition.put("end",end);
        mapCondition.put("attendence",attendence);

        try {
            PageHelper.startPage(pageNum,pageSize);
            List<Attendence1> attendenceList=attendenceService.findDate(mapCondition);
            PageInfo<Attendence1> pageInfo=new PageInfo<Attendence1>(attendenceList);
            map.put("pageInfo",pageInfo);
            map.put("attendenceList",attendenceList);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return  map;
    }
    /**
     * 批量增加跳转页面
     */
    @RequestMapping("addsJump")
    public String addsJump(Model model,HttpSession session){
        User1 user=(User1)session.getAttribute("user");
        //根据老师id，在班级表找到班级对象，
        List<Class> classList = classService.selectEmpByTeacherId(user.getUid());
        //班主任1对1,班级对象
        Class aClass = classList.get(0);
        //根据班级id，找到所有学生
        Student student=new Student();
        student.setClassId(aClass.getClassId());
        List<Student> studentList = studentService.selectExample(student);
        //根据班级id，在讲师课程表，找到讲师,找到课程
        Teachercourse teachercourse=new Teachercourse();
        teachercourse.setClassId(aClass.getClassId());
        List<Teachercourse1> teachercourseList = teacherCourseService.findByExample(teachercourse);
        model.addAttribute("teachercourseList",teachercourseList);
        model.addAttribute("aClass",aClass);

        return "WEB-INF/attendence/addmodel";
    }
    /**
     * 考勤表增加学生信息
     */
    @RequestMapping("adds")
    @ResponseBody
    public Map<String,Object> adds(Attendence attendence){

        Map<String, Object> map=new HashMap<String,Object>();
        try {
        //得到classID
        Integer clazzId=attendence.getClassId();
        //根据班级id，找到所有学生
        Student student=new Student();
        student.setClassId(clazzId);
        List<Student> studentList = studentService.selectExample(student);

        Attendence attendence0=new Attendence();
        attendence0.setCourseId(attendence.getCourseId());
        attendence0.setClassId(clazzId);
        if(attendence.getAttendenceDate()!=null&&!attendence.getAttendenceDate().equals("")){
            attendence0.setAttendenceDate(attendence.getAttendenceDate());
        }else{
            attendence0.setAttendenceDate(new Date());
        }
        attendence0.setAttendenceResult(attendence.getAttendenceResult());
        attendence0.setAttendenceReason("无");
        attendence0.setEmpId(attendence.getEmpId());
        for (Student stu:studentList) {
            attendence0.setStudentId(stu.getStudentId());
            attendenceService.add(attendence0);
        }
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return  map;
    }
    /**
     * 导出EXcel表格
     */
    /**
     * 根据条件将数据导出为Excel
     * 如果需要浏览器发送请求时即下载Excel，就不能用ajax进行传输，所以这里用GET方式进行提交
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("exportExcel")
    public Map<String,Object> exportExcel(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map=new HashMap<String,Object>();
        List<Attendence1> list=attendenceService.findAll();
        String[] title = {"ID", "学生", "班级", "讲师", "课程", "考勤日期", "结果", "缺勤原因"};
        String filename = "Attendence.xls";
        String sheetName = "sheet1";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String[][] content = new String[list.size()][8];
        try {
            for (int i = 0; i < list.size(); i++) {
                content[i][0] = String.valueOf(list.get(i).getAttendenceId());
                content[i][1] = list.get(i).getStudentId().getStudentName();
                content[i][2] = list.get(i).getClassId().getClassName();
                content[i][3] = list.get(i).getEmpId().getEmpName();
                content[i][4] = list.get(i).getCourseId().getCourseName();
                content[i][5] = sdf.format(list.get(i).getAttendenceDate());
                content[i][6] = list.get(i).getAttendenceResult();
                content[i][7] = list.get(i).getAttendenceReason();
                map.put("result","ok");
            }
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        HSSFWorkbook wb = TdExcelUtil.getHSSFWorkbook(sheetName, title, content, null);
        try {
            // 响应到客户端

            this.setResponseHeader(response, filename);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();

        }
        return map;
    }

    /**
     * 向客户端发送响应流方法
     *
     */
    public void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    @RequestMapping("importExcel")
    @ResponseBody
    public String importExcel(@RequestParam(value="attendenceExcel",required = false) MultipartFile attendenceExcel, HttpServletRequest request){

        String flag = "true";// 上传标志
        //导入
        try {
            flag = importExcelTd(attendenceExcel);
            System.out.println("######################");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    public String importExcelTd(MultipartFile attendenceExcel) throws IOException, InvalidFormatException {
        //获取输入流
        InputStream inputStream = attendenceExcel.getInputStream();
        //创建读取工作簿
        Workbook workbook = WorkbookFactory.create(inputStream);
        //获取工作表
        Sheet sheet = workbook.getSheetAt(0);
        //获取总行
        int rows=sheet.getPhysicalNumberOfRows();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if(rows>1){
            //获取单元格
            for (int i = 1; i < rows; i++) {
                Row row = sheet.getRow(i);
                Attendence attendence = new Attendence();

                try {
                    String studentId = row.getCell(1).getStringCellValue();
                    attendence.setStudentId(Integer.parseInt(studentId));
                } catch (IllegalStateException e) {
                    int studentId=(int)row.getCell(1).getNumericCellValue();
                    attendence.setStudentId(studentId);
                }
                try {
                    String classId = row.getCell(2).getStringCellValue();
                    attendence.setClassId(Integer.parseInt(classId));
                } catch (IllegalStateException e) {
                    int ClassId=(int)row.getCell(2).getNumericCellValue();
                    attendence.setClassId(ClassId);
                }
                try {
                    String empId = row.getCell(3).getStringCellValue();
                    attendence.setEmpId(Integer.parseInt(empId));
                } catch (IllegalStateException e) {
                    int empId=(int)row.getCell(3).getNumericCellValue();
                    attendence.setEmpId(empId);
                }
                try {
                    String courseId= row.getCell(4).getStringCellValue();
                    attendence.setCourseId(Integer.parseInt(courseId));
                } catch (IllegalStateException e) {
                    int courseId=(int)row.getCell(4).getNumericCellValue();
                    attendence.setCourseId(courseId);
                }
                try {
                    String attendenceDate = row.getCell(5).getStringCellValue();
                    attendence.setAttendenceDate(sdf.parse(attendenceDate));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                String attendenceResult= row.getCell(6).getStringCellValue();
                attendence.setAttendenceResult(attendenceResult);

                String attendenceReason = row.getCell(7).getStringCellValue();
                attendence.setAttendenceReason(attendenceReason);
                //想数据库中添加新对象
                System.out.println(attendence);

              attendenceService.add(attendence);
                System.out.println("(+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
                System.out.println(attendence);
                System.out.println("(+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
            }
        }
        inputStream.close();
        return "true";
    }

}
